package com.event.usc;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.connector.Request;

import com.event.usc.dto.Alert;
import com.event.usc.dto.EventAbb;
import com.event.usc.oracleJDBC.DatabaseHandler;
import com.google.gson.Gson;

/**
 * Servlet implementation class AlertServlet
 */
@WebServlet("/AlertServlet")
public class AlertServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String qtype = request.getParameter("q");
		if (qtype.equals("count"))
		{
			String userid = request.getParameter("userid");
			doCountAlertRequest(userid, response);
		}
		else if (qtype.equals("all"))
		{
			String userid = request.getParameter("userid");
			doAlertRequest(userid, response);
		}
		else if (qtype.equals("remove"))
		{
			long alertid = Long.parseLong(request.getParameter("alertid"));
			doRemoveRequest(alertid, response);
		}
		else if (qtype.equals("current"))
		{
			String userid = request.getParameter("userid");
			doCurrentRequest(userid, response);
		}
	}

	private void doCurrentRequest(String userid, HttpServletResponse response) {
		try {
			DatabaseHandler handler = new DatabaseHandler();
			long result = handler.getCurrent(userid);          
			printResponse(response, result);	
			handler.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
	}

	private void printResponse(HttpServletResponse response, long result) throws IOException {
		response.setContentType("text/html;charset=UTF-8");           
        response.setHeader("Cache-Control", "no-cache");
        PrintWriter out = response.getWriter();
        out.write("{ 'status' : '" + result +"' }");	
	}

	private void doRemoveRequest(long alertid,
			HttpServletResponse response) {
		try {
			DatabaseHandler handler = new DatabaseHandler(); 
			printResponse(response, handler.removeAlert(alertid));	
			handler.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
	}

	private void printResponse(HttpServletResponse response, boolean removeAlert) throws IOException {
		response.setContentType("text/html;charset=UTF-8");           
        response.setHeader("Cache-Control", "no-cache");
        PrintWriter out = response.getWriter();
        if (removeAlert)
        	out.write("{ 'status' : 'success' }");
        else
        	out.write("{ 'status' : 'error' }");
		
	}

	private void doCountAlertRequest(String userid, HttpServletResponse response) {
		try {
			DatabaseHandler handler = new DatabaseHandler(); 
			printResponse(response, handler.getCountAlerts(userid));	
			handler.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
	}

	private void printResponse(HttpServletResponse response, int countAlerts) throws IOException {
		response.setContentType("text/html;charset=UTF-8");           
        response.setHeader("Cache-Control", "no-cache");
        PrintWriter out = response.getWriter();
        out.write("{ 'status' : '" + countAlerts + "' }");
		
	}

	private void doAlertRequest(String userid, HttpServletResponse response) {
		try {
			DatabaseHandler handler = new DatabaseHandler();
			List<Alert> result = handler.getAlerts(userid);          
			printResponse(response, result);	
			handler.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private void printResponse(HttpServletResponse response, List<Alert> result) throws IOException {
		response.setContentType("text/html;charset=UTF-8");           
        response.setHeader("Cache-Control", "no-cache");
        PrintWriter out = response.getWriter();
        out.write(getOutString(result));
	}

	private String getOutString(List<Alert> result) {
		String outstring;
		if (result==null || result.size()==0)
        	outstring = "[]";
        else
        {
        	Gson gson = new Gson();
        	outstring = gson.toJson(result);
        }
		return outstring;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}
